package com.ycl.plus.lm.hardware.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ycl.plus.lm.common.core.domain.R;
import com.ycl.plus.lm.common.core.validate.AddGroup;
import com.ycl.plus.lm.common.core.validate.EditGroup;
import com.ycl.plus.lm.common.excel.utils.ExcelUtil;
import com.ycl.plus.lm.common.idempotent.annotation.RepeatSubmit;
import com.ycl.plus.lm.common.log.annotation.Log;
import com.ycl.plus.lm.common.log.enums.BusinessType;
import com.ycl.plus.lm.common.mybatis.core.domain.BaseChangeStatusEntity;
import com.ycl.plus.lm.common.mybatis.core.page.PageQuery;
import com.ycl.plus.lm.common.mybatis.core.page.TableDataInfo;
import com.ycl.plus.lm.common.web.core.BaseController;
import com.ycl.plus.lm.hardware.domain.bo.HardwareEquipBo;
import com.ycl.plus.lm.hardware.domain.vo.HardwareEquipVo;
import com.ycl.plus.lm.hardware.service.IHardwareEquipService;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 设备
 * 前端访问路由地址为:/hardware/equip
 *
 * @author devil-ycl
 * @date 2024-11-07
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/equip")
public class HardwareEquipController extends BaseController {

    private final IHardwareEquipService hardwareEquipService;

    /**
     * 查询设备列表
     */
    @SaCheckPermission("hardware:equip:list")
    @GetMapping("/list")
    public TableDataInfo<HardwareEquipVo> list(HardwareEquipBo bo, PageQuery pageQuery) {
        return hardwareEquipService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出设备列表
     */
    @SaCheckPermission("hardware:equip:export")
    @Log(title = "设备", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HardwareEquipBo bo, HttpServletResponse response) {
        List<HardwareEquipVo> list = hardwareEquipService.queryList(bo);
        ExcelUtil.exportExcel(list, "设备", HardwareEquipVo.class, response);
    }

    /**
     * 获取设备详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("hardware:equip:query")
    @GetMapping("/{id}")
    public R<HardwareEquipVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(hardwareEquipService.queryById(id));
    }

    /**
     * 新增设备
     */
    @SaCheckPermission("hardware:equip:add")
    @Log(title = "设备", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody HardwareEquipBo bo) {
        return toAjax(hardwareEquipService.insertByBo(bo));
    }

    /**
     * 修改设备
     */
    @SaCheckPermission("hardware:equip:edit")
    @Log(title = "设备", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody HardwareEquipBo bo) {
        hardwareEquipService.updateByBo(bo);
        return R.ok();
    }

    /**
     * 删除设备
     *
     * @param ids 主键串
     */
    @SaCheckPermission("hardware:equip:remove")
    @Log(title = "设备", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        hardwareEquipService.deleteWithValidByIds(List.of(ids), true);
        return R.ok();
    }

    /**
     * 激活设备
     *
     * @param ids 主键串
     */
    @SaCheckPermission("hardware:equip:edit")
    @Log(title = "设备", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("/active/{ids}")
    public R<Void> active(@NotEmpty(message = "id不能为空") @PathVariable Long[] ids) {
        hardwareEquipService.active(List.of(ids));
        return R.ok();
    }

    /**
     * 状态修改
     */
    @SaCheckPermission("hardware:equip:edit")
    @Log(title = "设备", businessType = BusinessType.UPDATE)
    @PutMapping("/changeRunStatus")
    public R<Void> changeStatus(@RequestBody BaseChangeStatusEntity<Integer> bo) {
        hardwareEquipService.updateStatus(bo.getId(), bo.getStatus());
        return R.ok();
    }

}
